#include "InteractivePlayFile.h"

static wxSemaphore	Sema;
static wxFile *pFile;
static int FileIndex;
static void* Tick(void *T)
{
	static int i=0;
	Sema.Post();
	i++;
	if(i == 250)
	{
		i = 0;
		Sema.Post();
		Sema.Post();
		Sema.Post();
		Sema.Post();
		Sema.Post();
		Sema.Post();
	}
	return NULL;
}

BEGIN_EVENT_TABLE( InteractivePlayFile, wxFrame )
	EVT_BUTTON(wxID_UP,InteractivePlayFile::OnUp)
	EVT_BUTTON(wxID_DOWN,InteractivePlayFile::OnDown)
	EVT_BUTTON(wxID_LEFT,InteractivePlayFile::OnLeft)
	EVT_BUTTON(wxID_RIGHT,InteractivePlayFile::OnRight)
	EVT_BUTTON(wxID_CENTER,InteractivePlayFile::OnCenter)
END_EVENT_TABLE()

InteractivePlayFile::InteractivePlayFile(wxWindow *pParent):FilePlayFrame(pParent)
{
	float Chann0,Chann1,Chann2,Chann3,Chann4;
	Instrument::Instrument();
	Resolution = 1;
	DigitalOffset = 1000;
	DigitalMax = 2000;
	DigitalMin = 0;
	MyName = _("Interactive file player");
	CurrentFile = 0;
	IsRunning = false;
	this->Show();
	pTimer		=	new timer(4,NULL,&Tick,NULL);

	File[0].Open(_T("Data\\Center.pla"));
	File[1].Open(_T("Data\\LEFT.pla"));
	File[2].Open(_T("Data\\RIGHT.pla"));
	File[3].Open(_T("Data\\UP.pla"));
	File[4].Open(_T("Data\\DOWN.pla"));

	pFile = &File[0];
	
	File[0].Read(&Chann0,sizeof(Chann0));
	File[1].Read(&Chann1,sizeof(Chann1));
	File[2].Read(&Chann2,sizeof(Chann2));
	File[3].Read(&Chann3,sizeof(Chann3));
	File[4].Read(&Chann4,sizeof(Chann4));
	wxASSERT_MSG(Chann0==Chann1 && Chann0 == Chann2 && Chann0==Chann3 &&  Chann0==Chann4,_("Interactive file player: Files are not compatible"));
	
	Initialize(Chann0,256,5);

}

InteractivePlayFile::~InteractivePlayFile(void)
{
	
}

void InteractivePlayFile::SelectFile(int Index)
{
	wxCriticalSection Region;
	Region.Enter();
	FileIndex = Index;
	pFile = &File[Index];
	Region.Leave();	
}



void* InteractivePlayFile::Entry(void)
{
	float		Dummy;
	int			SemaRet;
	bool		SynchOk = false;
	int			Dim;
	int			read;
	
	SampleStruct	Signal;
	memset(&Signal,0,sizeof(Signal));
	memset(&CurrentEvents,0,sizeof(CurrentEvents));
	Signal.Data.DataLen	=	Channels;
	
	wxCriticalSection Region;

	memset(&Signal,0,sizeof(Signal));
	Signal.Data.DataLen = Channels;
	//ch = new char[HowMany];
	read = 0;

	Esci = 0;
	while(!Esci)
	{		
		SemaRet = Sema.WaitTimeout(50);
		if(SemaRet !=wxSEMA_TIMEOUT && IsRunning)
		{
			Region.Enter();
			memset(&Signal,0,sizeof(Signal));
			Signal.Data.DataLen	=	Channels;
			
			for(int i = 0;i<Channels;i++)
			{
				Dim = File[FileIndex].Read(&Dummy,sizeof(float));
				if(Dim == 0)
				{
					File[FileIndex].Seek(0);
					Dim = File[FileIndex].Read(&Dummy,sizeof(float));//Channels
					Dim = File[FileIndex].Read(&Dummy,sizeof(float));
				}
				
				Signal.Data.Signal[i]=Dummy;
				
				
			}
			Region.Leave();
			PutSample(&Signal);
		}
	}

//	StopDevice();
	
	/*if(pDataSaver != NULL)
	{
		//	pDataSaver->Close();
		delete pDataSaver;
		pDataSaver = NULL;
	}*/

	File[0].Close();
	File[1].Close();
	File[2].Close();
	File[3].Close();
	File[4].Close();


	//Exit(0);*/
	return NULL;
}
void InteractivePlayFile::StartDevice(wxString FileName,wxString FilePath,bool File)
{
	Instrument::StartDevice(FileName,FilePath,File);
	FileIndex = 0;
	IsRunning	=	true;
	pTimer->start(PERIODIC);
}
void InteractivePlayFile::StopDevice(void)
{
	IsRunning	=	false;
	Instrument::StopDevice();
	pTimer->stop();
	Hide();
}
int InteractivePlayFile::OpenPort(const char *PortCom, const char *OpenVal2, const char *OpenVal3)
{

	return 1;
}